import { useBaseApi } from '/@/api/base';

// 知识库接口服务
export const useKnowledgeBaseApi = () => {
  const baseApi = useBaseApi("knowledgeBase");
  return {
    // 分页查询知识库
    getPage: baseApi.page,
    // 查看知识详情
    detail: baseApi.detail,
    // 新增知识
    add: baseApi.add,
    // 更新知识
    update: baseApi.update,
    // 删除知识
    delete: baseApi.delete,
    // 批量删除知识
    batchDelete: baseApi.batchDelete,
    // 搜索知识
    search: (data: any) => {
      return baseApi.request({
        url: baseApi.baseUrl + 'Search',
        method: 'get',
        params: data,
      });
    },
    // 获取分类
    getCategories: () => {
      return baseApi.request({
        url: baseApi.baseUrl + 'GetCategories',
        method: 'get',
      });
    },
    // 添加分类
    addCategory: baseApi.add,
    // 更新分类
    updateCategory: baseApi.update,
    // 删除分类
    deleteCategory: baseApi.delete,
    // 批量导入
    importKnowledge: (data: any) => {
      return baseApi.request({
        url: baseApi.baseUrl + 'ImportKnowledge',
        method: 'post',
        data,
      });
    },
    // 同步现有知识库到向量数据库（支持选择）
    syncExistingKnowledgeToVector: (data?: SyncVectorInput) => {
      return baseApi.request({
        url: baseApi.baseUrl + 'SyncExistingKnowledgeToVector',
        method: 'post',
        data,
      });
    },
    // 初始化向量数据库
    initializeQdrantCollection: () => {
      return baseApi.request({
        url: baseApi.baseUrl + 'InitializeQdrantCollection',
        method: 'post',
      });
    },
    // 获取Qdrant集合信息
    getQdrantCollectionInfo: () => {
      return baseApi.request({
        url: baseApi.baseUrl + 'qdrant/collection-info',
        method: 'get',
      });
    },
    // 获取Qdrant向量数量
    getQdrantPointsCount: (params?: { exact?: boolean }) => {
      return baseApi.request({
        url: baseApi.baseUrl + 'qdrant/points-count',
        method: 'get',
        params,
      });
    },
    // 滚动查看Qdrant点位
    scrollQdrantPoints: (params?: { limit?: number }) => {
      return baseApi.request({
        url: baseApi.baseUrl + 'qdrant/scroll-points',
        method: 'get',
        params,
      });
    },
  }
}

// RAGflow知识库同步接口服务
export const useRAGFlowSyncApi = () => {
  const baseApi = useBaseApi("ragFlowSync");
  return {
    // 获取RAGflow知识库列表
    getRAGFlowKnowledgeBases: () => {
      return baseApi.request({
        url: baseApi.baseUrl + 'GetRAGFlowKnowledgeBases',
        method: 'get',
      });
    },
    // 同步RAGflow知识库
    syncFromRAGFlow: (data: { kbId?: string }) => {
      return baseApi.request({
        url: baseApi.baseUrl + 'SyncFromRAGFlow',
        method: 'post',
        data,
      });
    },
  }
}

// RAGflow知识库实体
export interface RAGFlowKnowledgeBase {
  id: string;
  name: string;
  description?: string;
}

// 知识库实体
export interface KnowledgeBase {
  id?: number;
  title: string;
  content: string;
  category: string;
  keywords: string;
  documentType: string;
  source: string;
  documentUrl?: string;
  priority: number;
  isEnabled: boolean;
  lastUpdated: string;
  accessCount: number;
  similarityScore?: number;
  createTime?: string;
  updateTime?: string;
}

// 知识分类实体
export interface KnowledgeCategory {
  id?: number;
  name: string;
  description: string;
  parentId?: number;
  sortOrder: number;
  isEnabled: boolean;
  createTime?: string;
  updateTime?: string;
}

// 知识向量实体
export interface KnowledgeVector {
  id?: number;
  knowledgeId: number;
  vector: string;
  dimension: number;
  model: string;
  createTime?: string;
}

// 导入结果
export interface ImportResult {
  total: number;
  success: number;
  failed: number;
  errors: string[];
}

// 同步结果
export interface SyncResult {
  total: number;
  success: number;
  failed: number;
  errors: string[];
}

// 选择性同步输入
export interface SyncVectorInput {
  ids?: number[];
  missingOnly?: boolean;
}

// 知识导入DTO
export interface KnowledgeImportDto {
  title: string;
  content: string;
  category: string;
  keywords?: string;
  documentType?: string;
  source?: string;
  priority?: number;
}
